package udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UdpServer {
	public static void main(String[] args) throws IOException {
		byte[] buf = new byte[1024];
		DatagramSocket socket = new DatagramSocket(8002);
		DatagramPacket packet = new DatagramPacket(buf, 1024);
		socket.receive(packet);//block thread and listen port
		
		int length = packet.getLength();
		byte[] data = packet.getData();
		String string = new String(packet.getData(),0,packet.getLength());
		System.out.println(string);
		
		for(int i=0; i<length; i++){
			System.out.println(data[i]);
		}
		
		//confirm
		InetAddress address = packet.getAddress();
		int port = packet.getPort();
		System.out.println(address.toString()+"/"+port);
		
		DatagramPacket p1 = new DatagramPacket("ack".getBytes(), 0, 3, address, port);
		socket.send(p1);
		
		socket.close();
	}
}
